/**
 * version 1.1.7
 */
import apirequest from '../helpers/apiconfig.js'
import store from '../store'


class mysocket {
    constructor(config = {})
    {
        this.mineid = 0;
        this.sockettast = null;
        this.config = {node_site_url: 'ws://111.229.100.131:2348'};

    }

    connectserver(config)
    {
        console.info(config)
        let usign = config.puid
        this.mineid = config.puid;
        this.config = config;


        this.sockettast = uni.connectSocket({
            url: this.config.node_site_url,
            complete: function (res) {
                if (res.errMsg == 'connectSocket:ok')
                {
                    console.log('成功连接服务器')
                }
                else
                {
                    uni.showModal({
                        title: '链接服务器失败'
                    })
                }

            }
        })

		uni.onSocketOpen(function (res) {
		  console.log('WebSocket连接已打开！');
		});
		uni.onSocketError(function (res) {
		  console.log('WebSocket连接打开失败，请检查！');
		});


        this.sockettast.onMessage(function (res) {
            console.info('onMessage');

            let that = this;
            // let temdata=JSON.parse(res.data);
            let temdata = eval("(" + res.data + ")");
            console.info(temdata)

            switch (temdata.type)
            {
                case 'init':
                    apirequest.registClientid_f({
                        data: {
                            'cid': temdata.client_id,
                            'usign': usign
                        }
                    }).then(res => {
                        if (res.data.code != 1)
                        {
                            uni.showToast({
                                icon: 'none',
                                title: res.data.msg
                            })
                        }

                    });

                    break;
                case 'friend':
                    let msg = temdata;
                    let tempv_fid = 0;
                    store.commit("setCurrentMsg", msg)

                    tempv_fid = msg.id;

                    store.commit('updateChaterAttr', {
                        type: 'user',
                        val: msg.id,
                        attr: 'lastmsg',
                        data: msg.content,
                        time: 'msg.msg.time',
                        addinfo: {weidu:0, lastmsg:msg.content, id:msg.id}
                    })

                    let tempmy_id = {id: usign};
                    apirequest.cacheMessage(msg, 'user', msg.id, tempmy_id);

                    break;
                case 'group':
                    let gmsg = temdata;
                    let tempv = 0;
                    store.commit("setCurrentMsg", gmsg)
                    if (gmsg.sendmethod == 'group')
                    {
                        tempv = gmsg.toid;
                    }
                    else
                    {
                        tempv = gmsg.fromid;
                    }
                    store.commit('updateChaterAttr', {
                        type: gmsg.sendmethod,
                        val: tempv,
                        attr: 'lastmsg',
                        data: gmsg.content,
                        time: gmsg.msg.time,
                        addinfo: gmsg.msg.senderinfo
                    })

                    let tempmy = {id: usign};
                    apirequest.cacheMessage(msg, msg.sendmethod, tempv, tempmy);

                    break;
                case 'setfriendstue':
                    ///设置好友状态
                    let msg2 = temdata;
                    store.commit('setFriendstue', {val: msg2.toid, data: msg2.state, character: msg2.character})

                    break;
                case 'msgboxuser':
                    ///获取到信息盒子
                    store.commit('setmsgboxuser', temdata)
                    break;
                case 'msgboxgroup':
                    ///获取到信息盒子
                    store.commit('setmsgboxgroup', temdata)
                    break;
                case 'addgroup':
                    //自己成功创建群
                    store.commit('updateGrouplist', temdata)
                    break;
                case 'addfriend':
                    ///成功添加好友
                    store.commit('updateFriendlist', temdata)
                    break;


            }

            //设为已读
            /*
            var msg_row = JSON.parse(event.data);

            if (msg_row.message_id)
            {
                var url = sprintf("%s/account.php?mdu=sns&ctl=%s&met=%s&typ=json", SYS.CONFIG.base_url, 'User_Message', 'setRead');

                $.request({
                    type: 'post',
                    url: url,
                    data: {message_id:msg_row.message_id},
                    dataType: 'json',
                    success: function (result) {
                    }
                });

            }
             */
        })
    }

    sendMsg(data)
    {
        console.info('------sendMsg-------')
        console.info(data)

        //跨平台用户修正
        //data['to']['id'] = suid + '-' + data['to']['id'];
        //console.log(data);
        //发送消息
        if (this.sockettast)
        {
            //向服务器发送数据
            let text = JSON.stringify(data);


            uni.sendSocketMessage({
                data: text
            })
        }
    }
}

if (!global.$mysocket)
{
    global.$mysocket = new mysocket();
}

export default global.$mysocket;
